package com.example.myapplication;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private EditText edtTxtMainUserName;
    private EditText edtTxtMainPassword;
    private CheckBox chkBoxMainPSW;
    private CheckBox chkBoxMainName;
    private Button btnMainLogin;
    private Button btnMainReset;
    private SharedPreferences sp;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        sp = getSharedPreferences("login",MODE_PRIVATE);


        chkBoxMainName.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                //编辑器就是用来保存数据
                SharedPreferences.Editor editor = sp.edit();
                editor.putBoolean("chkName",isChecked);
                editor.commit();

            }
        });

        btnMainLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = edtTxtMainUserName.getText().toString().trim();
                String userpsw = edtTxtMainPassword.getText().toString().trim();
                if (username.equals("admin")&&userpsw.equals("123456")){
                    if (chkBoxMainName.isChecked()){//isChecked方法是复选框选中状态
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putString("username",username);
                        editor.commit();
                    }if (chkBoxMainPSW.isChecked()){//isChecked方法是复选框选中状态
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putString("userpsw",userpsw);
                        editor.commit();
                    }
                }
            }
        });





        chkBoxMainPSW.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                //编辑器就是用来保存数据
                SharedPreferences.Editor editor = sp.edit();
                editor.putBoolean("chkPsw",isChecked);
                editor.commit();

            }
        });

    }

     protected void onStart(){//这个方法实现的是在ChkBox上显示打勾
        super.onStart();
        boolean chkName = sp.getBoolean("chkName",false);
        chkBoxMainName.setChecked(chkName);

        boolean chkPsw = sp.getBoolean("chkPsw",false);
        chkBoxMainPSW.setChecked(chkPsw);

      if (chkName){
          edtTxtMainUserName.setText(sp.getString("username",null));
      }if (chkPsw){
             edtTxtMainPassword.setText(sp.getString("userpsw",null));
         }



    }

    private void initView() {
        textView = (TextView) findViewById(R.id.textView);
        edtTxtMainUserName = (EditText) findViewById(R.id.edtTxtMainUserName);
        edtTxtMainPassword = (EditText) findViewById(R.id.edtTxtMainPassword);
        chkBoxMainPSW = (CheckBox) findViewById(R.id.chkBoxMainPSW);
        chkBoxMainName = (CheckBox) findViewById(R.id.chkBoxMainName);
        btnMainLogin = (Button) findViewById(R.id.btnMainLogin);
        btnMainReset = (Button) findViewById(R.id.btnMainReset);
    }
}